﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using Forms = System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Timers;
using NPOI.SS.Formula.Functions;

namespace GpCommon.CustomControl
{
    public enum MsgType
    {
        /// <summary>
        /// 封单量骤减
        /// </summary>
        VolumeDrop,
        /// <summary>
        /// 打开涨停
        /// </summary>
        TopOpen,
        /// <summary>
        /// 打开跌停
        /// </summary>
        BottomOpen,
        /// <summary>
        /// 猛烈打压
        /// </summary>
        FastDrop,
        /// <summary>
        /// 急速拉升
        /// </summary>
        FastRise,
        /// <summary>
        /// 封涨停板
        /// </summary>
        TopSeal,
        /// <summary>
        /// 封跌停板
        /// </summary>
        BottomSeal
    }
    public class MsgEntity
    {
        public string Code { get; set; }
        public MsgType Type { get; set; }
        public DateTime Time { get; set; }
        public bool IsAlive { get; set; } = true;
    }
    /// <summary>
    /// MessageView.xaml 的交互逻辑
    /// </summary>
    public partial class MessageView : Window
    {
        private static List<MsgEntity> AliveMsgList = new List<MsgEntity>();
        public static bool IsShowMsg = false;
        private Storyboard _storyboard = null;
        private System.Threading.SynchronizationContext synContent = null;
        private string m_Code;
        private MsgType m_Type;
        public static void Create(string msg)
        {
            if (IsShowMsg)
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    MessageView view = new MessageView(msg);
                    view.Show();
                });

            }
        }
        public static void CreateMsg(string code, string name, MsgType type)
        {
            if (IsShowMsg && CanShow(code, type))
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    string msg = "";
                    switch(type)
                    {
                        case MsgType.VolumeDrop:
                            msg = $"{name}({code})封单量骤减！";
                            break;
                        case MsgType.TopOpen:
                            msg = $"{name}({code})打开涨停！";
                            break;
                        case MsgType.BottomOpen:
                            msg = $"{name}({code})打开跌停！";
                            break;
                        case MsgType.FastDrop:
                            msg = $"{name}({code})快速下跌！";
                            break;
                        case MsgType.FastRise:
                            msg = $"{name}({code})快速拉升！";
                            break;
                        case MsgType.TopSeal:
                            msg = $"{name}({code})封涨停！";
                            break;
                        case MsgType.BottomSeal:
                            msg = $"{name}({code})封跌停！";
                            break;
                    }
                    
                    MessageView view = new MessageView(msg);
                    view.m_Code = code;
                    view.m_Type = type;
                    view.Show();
                });
                var temp = AliveMsgList.Find(x => x.Code == code && x.Type == type);
                if(temp == null)
                {
                    MsgEntity entity = new MsgEntity();
                    entity.Code = code;
                    entity.Type = type;
                    entity.Time = DateTime.Now;
                    entity.IsAlive = true;
                    AliveMsgList.Add(entity);
                }
                else
                {
                    temp.Time = DateTime.Now;
                    temp.IsAlive = true;
                }
            }
        }
        public MessageView(string msg)
        {
            InitializeComponent();

            this.MsgText.Text = msg;
            this.Top = Forms.Screen.PrimaryScreen.Bounds.Bottom - this.Height - 50;
            this.Left = Forms.Screen.PrimaryScreen.Bounds.Right - this.Width;

            synContent = System.Threading.SynchronizationContext.Current;

            ShowAnimation();
        }
        private void TimeExecute(object sender, ElapsedEventArgs e)
        {
            synContent.Post(LeaveAnimation, null);
        }
        private void ShowAnimation()
        {
            _storyboard = new Storyboard();

            //创建Y轴动画
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = Forms.Screen.PrimaryScreen.Bounds.Bottom;
            doubleAnimation.To = this.Top;
            doubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(1000));

            Storyboard.SetTarget(doubleAnimation, this);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Window.Top)"));

            _storyboard.Children.Add(doubleAnimation);
            _storyboard.Completed += ShowComplete;
            _storyboard.Begin();
        }
        private void LeaveAnimation(object e)
        {
            _storyboard = new Storyboard();

            //创建X轴动画
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = this.Left;
            doubleAnimation.To = Forms.Screen.PrimaryScreen.Bounds.Right;
            doubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(1000));

            Storyboard.SetTarget(doubleAnimation, this);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Window.Left)"));

            _storyboard.Children.Add(doubleAnimation);
            _storyboard.Completed += LeaveComplete;
            _storyboard.Begin();
        }

        private void ShowComplete(object sender, EventArgs e)
        {
            _storyboard.Stop();

            Timer time = new Timer();
            time.Interval = 8000;
            time.Elapsed -= TimeExecute;
            time.Elapsed += TimeExecute;
            time.Start();
        }
        private void LeaveComplete(object sender, EventArgs e)
        {
            _storyboard.Stop();
            this.Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(m_Code))
            {
                var temp = AliveMsgList.Find(x => x.Code == m_Code && x.Type == m_Type);
                if (temp != null)
                {
                    temp.IsAlive = false;
                }
            }
        }


        private static bool CanShow(string code, MsgType type)
        {
            var temp = AliveMsgList.Find(x => x.Code == code && x.Type == type);
            if (temp == null || (!temp.IsAlive && DateTime.Now - temp.Time >= new TimeSpan(0, 3, 0)))
            {
                return true;
            }
            return false;
        }
    }
}
